package org.csu.mypetstore.api.controller;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.service.OrderService;
import org.csu.mypetstore.api.vo.CartVO;
import org.csu.mypetstore.api.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private CatalogService catalogService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private AccountService accountService;


    @GetMapping("/login/{username}")
    @ResponseBody
    public Object login(@PathVariable String username, HttpSession session) {
        CartVO persisCart = cartService.getCart(username);
        CartVO tempCart = (CartVO) session.getAttribute("cart");
        session.setAttribute("cart", cartService.mergeCarts(tempCart, persisCart));
        return accountService.getAccount(username);
    }

    @GetMapping("/logout")
    @ResponseBody
    public Object logout(HttpSession session) {
        CartVO cartVO = new CartVO();
        session.setAttribute("cart", cartVO);
        return CommonResponse.createForSuccess(cartVO);
    }

    @GetMapping("/cart_test1")
    @ResponseBody
    public Object getCart(HttpSession session) {
        CartVO cartVO = new CartVO();
        session.setAttribute("cart", cartVO);
        return CommonResponse.createForSuccess(cartVO);
    }

    @GetMapping("/cart_test2/{itemId}")
    @ResponseBody
    public Object addItemToCart(HttpSession session, @PathVariable String itemId) {
        CartVO cartVO = (CartVO) session.getAttribute("cart");
        if (cartVO == null) return CommonResponse.createForError("没有购物车");
        cartService.addCartItem(cartVO, itemId);
        session.setAttribute("cart", cartVO);
        return CommonResponse.createForSuccess(cartVO);
    }

    @GetMapping("/cart_test3/{itemId}")
    @ResponseBody
    public Object deleteItemToCart(HttpSession session, @PathVariable String itemId) {
        CartVO cartVO = (CartVO) session.getAttribute("cart");
        if (cartVO == null) return CommonResponse.createForError("没有购物车");
        cartService.deleteCartItem(cartVO, itemId);
        session.setAttribute("cart", cartVO);
        return CommonResponse.createForSuccess(cartVO);
    }

    @GetMapping("/cart_test4/{itemId}/{qty}")
    @ResponseBody
    public Object updateItemToCart(HttpSession session, @PathVariable String itemId, @PathVariable int qty) {
        CartVO cartVO = (CartVO) session.getAttribute("cart");
        if (cartVO == null) return CommonResponse.createForError("没有购物车");
        cartService.updateCartItemQty(cartVO, qty, itemId);
        session.setAttribute("cart", cartVO);
        return CommonResponse.createForSuccess(cartVO);
    }

    @GetMapping("/order_test1")
    @ResponseBody
    public Object checkout(HttpSession session, OrderVO orderVO) {
        CartVO cartVO = (CartVO) session.getAttribute("cart");
        if (cartVO == null) return CommonResponse.createForError("没有购物车");
        return orderService.initOrder(cartVO, orderVO);
    }
}
